package com.test.satokenmongodbdemo;

import cn.dev33.satoken.stp.StpUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping
public class IndexController {

    @GetMapping("/login")
    public String login() {
        String loginId = "test-user";
        StpUtil.login(loginId);

        return loginId;
    }

    @GetMapping("/index")
    public String index() {

        Object count = StpUtil.getSession().get("count");

        int views = 1;

        if (count instanceof Integer integer) {
            views = integer + 1;
        }

        StpUtil.getSession().set("count", views);

        return StpUtil.getLoginIdAsString() + " session:" + views;
    }

    @GetMapping("/logout")
    public String logout() {

        StpUtil.logout();

        return "logout";
    }
}
